package com.henghe.frame.core.async;

import com.henghe.frame.core.util.SpringUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.TimerTask;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2023/3/20 14:57
 */
public class AsyncManager {

    /**
     * 操作延迟10毫秒
     */
    private final int OPERATE_DELAY_TIME = 10;

    /**
     * 异步操作任务调度线程池
     */
    private ThreadPoolTaskExecutor executor = SpringUtils.getBean("asyncServiceExecutor");

    /**
     * 单例模式
     */
    private AsyncManager() {
    }

    private static AsyncManager me = new AsyncManager();

    public static AsyncManager me() {
        return me;
    }

    /**
     * 执行任务
     *
     * @param task 任务
     */
    public void execute(TimerTask task) {
        executor.execute(task, OPERATE_DELAY_TIME);
    }

    /**
     * 停止任务线程池
     */
    public void shutdown() {
        executor.shutdown();
//        Threads.shutdownAndAwaitTermination(executor);
    }
}
